package com.example.controller;

import cn.hutool.core.util.ObjectUtil;
import com.example.common.Result;
import com.example.common.ResultCode;
import com.example.dao.TeacherInfoDao;
import com.example.entity.Account;
import com.example.entity.Admininfo;
import com.example.entity.StudentInfo;
import com.example.entity.TeacherInfo;
import com.example.exception.CustomException;
import com.example.service.AdminInfoService;
import com.example.service.StudentInfoService;
import com.example.service.TeacherInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author 涛某
 * @version 1.0
 * @date 2024/3/17 11:49
 * 和账号相关的接口都在这里
 */
@RestController
@RequestMapping()
public class AccountController {
    @Autowired
    private AdminInfoService adminInfoService;
    @Autowired
    private TeacherInfoService teacherInfoService;
   /*
    private TeacherInfoDao teacherInfoDao;*/
    @Autowired
    private StudentInfoService studentInfoService;

    /**
     * 登录功能
     * @param user 登录用户的信息
     * @param request request 请求
     * @return Result
     */
    @PostMapping("/login")
    public Result login(@RequestBody Account user,HttpServletRequest request){

        //首先要校验数据有没有填入
        if (ObjectUtil.isEmpty(user.getName()) || ObjectUtil.isEmpty(user.getPassword()) || ObjectUtil.isEmpty(user.getLevel())){
            return Result.error("-1","请完善输入的信息");
        }
        Integer level = user.getLevel();
        Account loginuser = new Account();
        if (level==1){
            //管理员登录
          loginuser = adminInfoService.login(user.getName(),user.getPassword());
        }
        if (level==2){
            //教师登录
          loginuser =  teacherInfoService.login(user.getName(),user.getPassword());
        }
        if (level==3){
            //学生登录
            loginuser =  studentInfoService.login(user.getName(),user.getPassword());
        }

        //在session中把用户信息存一份

        request.getSession().setAttribute("user",loginuser);
        return Result.success(loginuser);
    }

    //
    @GetMapping("/getUser")
    public Result getUser(HttpServletRequest request){
        //先从session  获取当前存储的登录用户的信息
        Account user = (Account) request.getSession().getAttribute("user");
        //判断对当前登录的用户的角色
        Integer level = user.getLevel();
        if (level == 1 ){
            //获取管理员
           Admininfo admininfo =  adminInfoService.FindById(user.getId());
           return Result.success(admininfo);
        }
        if (level==2){
            //从教师表里获取教师信息
           TeacherInfo teacherInfo =  teacherInfoService.FindById(user.getId());
            return Result.success(teacherInfo);
        }
        if (level==3){
            //从学生表里获取学生信息
          StudentInfo studentInfo =  studentInfoService.FindById(user.getId());
            return Result.success(studentInfo);

        }
        return Result.success(new Account());
    }

    //注册
    @PostMapping("/register")
    public Result register(HttpServletRequest request, @RequestBody Account user){

        //先校验数据有没有填入
        //如果没有填入参数则抛出一个参数缺失的信息

        if (ObjectUtil.isEmpty(user.getName())||ObjectUtil.isEmpty(user.getPassword())||ObjectUtil.isEmpty(user.getLevel())){
            return Result.error("-1","请输入完整的信息");
        }
        Integer level = user.getLevel();
        //教师注册
        if (2 == level){
            TeacherInfo teacherInfo = new TeacherInfo();
            BeanUtils.copyProperties(user,teacherInfo);
            teacherInfoService.register(teacherInfo);
        }
        //学生注册
        if (3 == level){
            StudentInfo studentInfo = new StudentInfo();
            BeanUtils.copyProperties(user,studentInfo);
            studentInfoService.register(studentInfo);

        }


        return Result.success();

    }
    //退出登录
    @GetMapping("/logout")
    public Result logout(HttpServletRequest request){
     //   Account user = (Account) request.getSession().getAttribute("user");
        request.getSession().setAttribute("user",null);
        return Result.success();
    }
}
